﻿using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Provider;
using CE.CarInsurance;
using CE.Company;
using CE.Mail;
using CE.InputOut;
using System.Net;
using CE.Security;

public partial class car_insurance_compare_insurance : MyLanguageBasePage
{
    string _redirectUrl = "";
    string _strNewEmail = "";
    string _strNewName = "";
    string _strNewMobile = "";
    string _strNewUserId = "";
    string _strUserStatus = "";

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.Page.Title = Resources.Title.CompareCIQuotesTitle;
        if (!Page.IsPostBack)
        {
            txtEmailPop.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmailPop.ClientID + "','" + rfvEmailPop.ClientID + "','" + revEmailPop.ClientID + "');");
            txtNamePop.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtNamePop.ClientID + "','" + rfvNamePop.ClientID + "','" + revNamePop.ClientID + "');");
            txtMobilePop.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtMobilePop.ClientID + "','" + rfvMobilePop.ClientID + "','" + revMobilePop.ClientID + "');");
            ddlCallMeTime.Attributes.Add("onblur", "ShowValidationEffect('" + ddlCallMeTime.ClientID + "','" + rfvCallMeTime.ClientID + "');");

            if (Session[CE.Global.General.UserEmail] != null)
            {
                txtEmailPop.Text = Session[CE.Global.General.UserEmail].ToString();
            }
            if (Session[CE.Global.General.UserName] != null)
            {
                txtNamePop.Text = Session[CE.Global.General.UserName].ToString();
            }
            if (Session[CE.Global.General.UserPhoneNo] != null)
            {
                txtMobilePop.Text = Session[CE.Global.General.UserPhoneNo].ToString();
            }
            if (Session["CITempResultId"] != null && Session["CITempResultId"].ToString() != "")
            {
                ShowQuoteSummary();
                BindRptCompare();
            }
            else
            {
                Response.Redirect(CE.Global.General.GetUrl("/car-insurance/car-details", Page));
            }
            Int64 _carId = 0;
            if (Session[General.CarId] != null)
            {
                if (!Int64.TryParse(Session[General.CarId].ToString(), out _carId))
                {
                    _carId = 0;
                }
            }
            GetPromotionDetails(_carId);
        }
    }

    private void BindRptCompare()
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblCarModel");
        SortedList _spList = new SortedList();
        _spList.Add("@CarResultIdList", Session["CITempResultId"].ToString());
        _dt = _db.GetDataTableBySP("sprocGetCompareInsuranceListByTempId", _spList);
        if (_dt != null && _dt.Rows.Count > 0)
        {
            rptCompare.DataSource = _dt;
            rptCompare.DataBind();
        }

        _dt = null;
        _db = null;
    }

    protected void rptCompare_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        Label lblProviderId = (Label)e.Item.FindControl("lblProviderId");
        Label lblTempResultId = (Label)e.Item.FindControl("lblTempResultId");
        Label lblPremium = (Label)e.Item.FindControl("lblPremium");
        string _redirectUrl;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
        }
        if (e.CommandName.ToLower().Equals("buynow"))
        {
            Session[CE.Global.General.ProviderCompanyId] = lblProviderId.Text;
            Session[CE.Global.General.CarResultId] = lblTempResultId.Text;
            _redirectUrl = SetBuyNowUrl(lblProviderId.Text);
            Response.Redirect(_redirectUrl);
        }
        else if (e.CommandName.ToLower().Equals("moreinfo"))
        {
            Session[CE.Global.General.ProviderCompanyId] = lblProviderId.Text;
            Session[CE.Global.General.CarResultId] = lblTempResultId.Text;

            //_redirectUrl = CE.Global.General.GetUrl("~/car-insurance/quote-details.aspx", Page);
            //Response.Redirect(_redirectUrl);

        }
        else if (e.CommandName.ToLower().Equals("callnow"))
        {
            if (CheckCallMeCount() < 3)
            {
                Session[CE.Global.General.ProviderCompanyId] = lblProviderId.Text;
                Session[CE.Global.General.CarResultId] = lblTempResultId.Text;
                string strInsType = "";
                if (lblInsuranceType.Value == "1")
                {
                    strInsType = "10";
                }
                else if (lblInsuranceType.Value == "2+")
                {
                    strInsType = "21";
                }
                else if (lblInsuranceType.Value == "3")
                {
                    strInsType = "30";
                }
                else if (lblInsuranceType.Value == "3+")
                {
                    strInsType = "31";
                }
                divBrandPopup.Attributes.Add("class", GetMotorBrand(lblCarMakeCode.Value));
                divInsurerPopup.Attributes.Add("class", getProviderLogoCss(lblProviderId.Text, "moreinfo"));
                divMakeModelNamePopup.InnerHtml = lblCarMakeName.Value + " " + lblCarModelFamilyName.Value;
                divPremiumPopup.InnerHtml = lblPremium.Text;
                hidProvider.Value = lblProviderId.Text;
                ShowCallPopup();
                mpeCallPop.Show();
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", "alert('" + Resources.General.Callback3Times + "');", true);
            }
        }
    }

    protected void rptCompare_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lblPackageName = (Label)e.Item.FindControl("lblPackageName");
            Label lblProviderId = (Label)e.Item.FindControl("lblProviderId");
            Image imgProvider = (Image)e.Item.FindControl("imgProvider");
            HtmlGenericControl divProvider = (HtmlGenericControl)(e.Item.FindControl("divProvider"));
            HtmlGenericControl divSpecialNCB = (HtmlGenericControl)e.Item.FindControl("divSpecialNCB");
            if (lblPackageName.Text == "GluayGluay.com Special")
            {
                imgProvider.ImageUrl = "/images/gg-logo/gluaygluay-logo.png";
                divSpecialNCB.Style.Add("display", "");
                imgProvider.Visible = true;
                divProvider.Visible = false;
            }
            else
            {
                imgProvider.Visible = false;
                divProvider.Visible = true;
            }
            divProvider.Attributes.Add("class", getProviderLogoCss(lblProviderId.Text, "result"));
        }
        Repeater ss = (Repeater)sender;

    }

    public string SetBuyNowUrl(string providerid)
    {
        string _redirectUrl = string.Empty;
        _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/proposal-form.aspx", Page);
        //if (providerid == "11")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/azcp-proposal-form.aspx", Page);
        //}
        //else if (providerid == "12")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/msig-proposal-form", Page);
        //}
        //else if (providerid == "16")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/axa-proposal-form", Page);
        //}
        //else if (providerid == "29")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/lmg-proposal-form", Page);
        //}
        //else if (providerid == "31")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/aig-proposal-form", Page);
        //}
        //else if (providerid == "32")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/bki-proposal-form", Page);
        //}
        //else if (providerid == "33")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/mti-proposal-form", Page);
        //}
        //else if (providerid == "35")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/krungthai-proposal-form", Page);
        //}
        //else if (providerid == "39")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/mittare-proposal-form", Page);
        //}
        //else if (providerid == "41")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/ksk-proposal-form", Page);
        //}
        //else if (providerid == "43")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/synmunkong-proposal-form", Page);
        //}
        //else if (providerid == "45")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/viriyah-proposal-form", Page);
        //}
        //else if (providerid == "46")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/tms-proposal-form", Page);
        //}
        //else if (providerid == "51")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/scsmg-proposal-form", Page);
        //}
        //else if (providerid == "58")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/tvv-proposal-form", Page);
        //}
        //else if (providerid == "65")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/dhipaya-proposal-form", Page);
        //}
        //else if (providerid == "66")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/thaipaiboon-proposal-form", Page);
        //}
        //else if (providerid == "67")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/deves-proposal-form.aspx", Page);
        //}
        //else if (providerid == "68")
        //{
        //    _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/proposalforms/thaisri-proposal-form.aspx", Page);
        //}
        return _redirectUrl;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtMobilePop.Text.Trim().Length == 10)
            {
                string _message = "";
                string _title = "";
                string CallMeFullNamePop = "";
                string strInsType = "";
                string ExistingEmail = Session[General.UserEmail].ToString();

                ChangeValidationEffectServerSide("callme");
                if (IsValid)
                {
                    //Sakthi - Added on 24 Oct 2014 - Start
                    Int64 _UserId = 0;
                    Int64 _CarId = 0;
                    Int64 _ExistingUserId = 0;
                    if (Session[CE.Global.General.CarId] != null)
                    {
                        if (!Int64.TryParse(Session[CE.Global.General.CarId].ToString(), out _CarId))
                        {
                            _CarId = 0;
                        }
                    }
                    if (Session[CE.Global.General.UserId] != null)
                    {
                        if (!Int64.TryParse(Session[General.UserId].ToString(), out _ExistingUserId))
                        {
                            _ExistingUserId = 0;
                        }
                    }

                    Account _useraccount = new Account();
                    AccountDB _useraccountdb = new AccountDB();
                    _useraccount = _useraccountdb.GetUserAccount(txtEmailPop.Text.Trim(), Convert.ToInt32(CE.Global.CEUserType.Customer));
                    //Hide on 13 Nov 2014 //_useraccount = _useraccountdb.GetUserAccount(txtEmailPop.Text.Trim(), txtMobilePop.Text.Trim().ToLower(), Convert.ToInt32(CE.Global.CEUserType.Customer));
                    if (_useraccount != null && _useraccount.CEID > 0)
                    {
                        Session[CE.Global.General.UserId] = _useraccount.CEID.ToString();
                        Session[CE.Global.General.UserTypeId] = _useraccount.UserTypeId;
                        Session[CE.Global.General.UserEmail] = _useraccount.Email;
                        _UserId = _useraccount.CEID;
                        UpdateProfile(_UserId, _CarId);
                    }
                    else
                    {
                        General.ClearAllSession(this.Page, "USER");
                        UpdateProfile(_UserId, _CarId);
                        if (Session[CE.Global.General.UserId] != null)
                        {
                            if (!Int64.TryParse(Session[CE.Global.General.UserId].ToString(), out _UserId))
                            {
                                _UserId = 0;
                            }
                        }
                        _UserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                        UpdateUserIdCarDetails(_CarId, _UserId, _ExistingUserId, ExistingEmail, "SWITCH", txtEmailPop.Text.Trim().ToLower(), Session[General.UserName].ToString(), txtMobilePop.Text.Trim());
                        SendMailToUser(txtEmailPop.Text.Trim().ToLower(), Session[General.UserName].ToString(), txtMobilePop.Text.Trim());
                        _strUserStatus = "SWITCH";
                    }
                    //Sakthi - Added on 24 Oct 2014 - End

                    CallMeFullNamePop = Session[General.UserName].ToString();
                    _message = "Customer Name: " + Server.HtmlEncode(CallMeFullNamePop);
                    _title = "Call Me Now - Call Me request by " + Server.HtmlEncode(CallMeFullNamePop);
                    SetCookies(Server.HtmlEncode(txtNamePop.Text.Trim()), txtEmailPop.Text.Trim().ToLower(), txtMobilePop.Text.Trim());
                    Int64 _result = 0;
                    ContactUser _cu = new ContactUser();
                    ContactUserDB _cudb = new ContactUserDB();

                    strInsType = lblInsuranceType.Value;

                    _cu.InsuranceType = "CI-" + strInsType;


                    _cu.ProductId = _CarId;
                    _cu.RefCEUserId = _UserId;

                    _cu.ProviderCompanyId = Convert.ToInt64(hidProvider.Value);
                    _cu.ContactNo = txtMobilePop.Text.Trim();
                    _cu.FullName = Server.HtmlEncode(CallMeFullNamePop);
                    _cu.CallTime = Convert.ToInt16(ddlCallMeTime.Text);

                    if (Request.Params["spc"] != null)
                    {
                        if (Request.Params["spc"].ToString() == "1")
                        {
                            _cu.Info1 = "GG Promotion";
                        }
                    }
                    if (hdnPromoId.Value == "4" && hdnPromoCode.Value != "0")
                    {
                        _cu.Info4 = Server.HtmlEncode(hdnPromoCode.Value.Trim());
                        _cu.NInfo5 = Convert.ToInt32(hdnPromoId.Value.Trim());
                    }


                    _result = _cudb.Insert(_cu);

                    if (_result > 0)
                    {
                        #region Hide on 23 Oct 2014
                        //Profile _profile = new Profile();
                        //ProfileDB _profiledb = new ProfileDB();
                        //_profile = _profiledb.GetProfile(_cu.RefCEUserId);
                        //if (_profile != null && _profile.ProfileId > 0)
                        //{
                        //    _profile.SurName = string.Empty;
                        //    _profile.FirstName = Server.HtmlEncode(CallMeFullNamePop);
                        //    _profiledb.Insert(_profile);
                        //}
                        //_profile = null;
                        //_profiledb = null;

                        //Contact _contact = new Contact();
                        //ContactDB _contactdb = new ContactDB();
                        //_contact = _contactdb.GetContact(_cu.RefCEUserId);
                        //if (_contact != null && _contact.ContactId > 0)
                        //{
                        //    _contact.Mobile = txtMobilePop.Text.Trim();
                        //    _contactdb.Insert(_contact);
                        //}
                        //_contact = null;
                        //_contactdb = null;

                        //CE.CarInsurance.CarDetailTH _cardetail = new CE.CarInsurance.CarDetailTH();
                        //CE.CarInsurance.CarDetailTHDB _cardetailDB = new CE.CarInsurance.CarDetailTHDB();
                        //_cardetail = _cardetailDB.GetCar(_cu.ProductId, _cu.RefCEUserId);
                        //if (_cardetail != null && _cardetail.CarId > 0)
                        //{
                        //    _cardetail.Info7 = string.Empty;
                        //    _cardetail.Info8 = Server.HtmlEncode(CallMeFullNamePop);
                        //    _cardetailDB.Insert(_cardetail);
                        //}
                        //_cardetail = null;
                        //_cardetailDB = null;
                        #endregion
                        ScriptManager.RegisterStartupScript(Page, this.GetType(), "alertThxU", "alert('" + Resources.CarInsurance.CallMeMessage + "')", true);

                        #region Hide Call me email sending - Sakthi on 11 Dec 2014
                        //_message += "<img src='http://www.gluaygluay.com/images/callme/callme-home.png' /><br/>";
                        //_message += "<br/><br/>Insurance Type : " + strInsType;
                        //_message += "<br/><br/>Full Name : " + Server.HtmlEncode(CallMeFullNamePop);
                        //_message += "<br/><br/>Email : " + txtEmailPop.Text;
                        //_message += "<br/><br/>Contact No : " + txtMobilePop.Text.Trim();
                        //_message += "<br/><br/>Call Time : " + ddlCallMeTime.SelectedItem.Text;
                        //string _emailList = ConfigurationManager.AppSettings["TH.CICallMeMessageReceiver"].ToString();
                        //MyMailManager.SendCRMMessage(_emailList, _title, _message);
                        #endregion
                        //divCallMe.Visible = true;
                        //lblCallMeMessage.Text = Resources.CarInsurance.CallMeMessage;

                        ddlCallMeTime.SelectedIndex = 0;
                        //txtEmailPop.ReadOnly = true;
                        //ddlCallMeTime.Enabled = false;
                        //txtMobilePop.ReadOnly = true;
                        //btnSubmit.Enabled = false;
                        btnCancel_Click(null, null);
                        mpeCallPop.Hide();
                        //if (_strUserStatus == "CHANGE" || _strUserStatus == "SWITCH")
                        //{
                        //    Response.Redirect(CE.Global.General.GetUrl("/car-insurance/results", Page));
                        //}
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        mpeCallPop.Hide();
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        txtEmailPop.Text = HttpUtility.HtmlEncode(txtEmailPop.Text);
        txtNamePop.Text = HttpUtility.HtmlEncode(txtNamePop.Text);
        txtMobilePop.Text = HttpUtility.HtmlEncode(txtMobilePop.Text);

        #region Customer
        //Email
        if (rfvEmailPop.Enabled && rfvEmailPop.ValidationGroup == strValidationGroup)
        {
            rfvEmailPop.Validate();
            if (rfvEmailPop.IsValid == false)
            {
                txtEmailPop.ControlStyle.CssClass = "cb-fields validationError";
            }
            else
            {
                txtEmailPop.ControlStyle.CssClass = "cb-fields";
            }
        }
        else
        {
            txtEmailPop.ControlStyle.CssClass = "cb-fields";
        }

        if (revEmailPop.Enabled && revEmailPop.ValidationGroup == strValidationGroup)
        {
            if (rfvEmailPop.IsValid)
            {
                revEmailPop.Validate();
                if (revEmailPop.IsValid == false)
                {
                    txtEmailPop.ControlStyle.CssClass = "cb-fields validationError";
                }
                else
                {
                    txtEmailPop.ControlStyle.CssClass = "cb-fields";
                }
            }
        }
        else
        {
            txtEmailPop.ControlStyle.CssClass = "cb-fields";
        }

        //MobileNo
        if (rfvMobilePop.Enabled && rfvMobilePop.ValidationGroup == strValidationGroup)
        {
            rfvMobilePop.Validate();
            if (rfvMobilePop.IsValid == false)
            {
                txtMobilePop.ControlStyle.CssClass = "cb-fields validationError";
            }
            else
            {
                txtMobilePop.ControlStyle.CssClass = "cb-fields";
            }
        }
        else
        {
            txtMobilePop.ControlStyle.CssClass = "cb-fields";
        }

        if (revMobilePop.Enabled && revMobilePop.ValidationGroup == strValidationGroup)
        {
            if (rfvMobilePop.IsValid)
            {
                revMobilePop.Validate();
                if (revMobilePop.IsValid == false)
                {
                    txtMobilePop.ControlStyle.CssClass = "cb-fields validationError";
                }
                else
                {
                    txtMobilePop.ControlStyle.CssClass = "cb-fields";
                }
            }
        }
        else
        {
            txtMobilePop.ControlStyle.CssClass = "cb-fields";
        }

        //Name
        if (rfvNamePop.Enabled && rfvNamePop.ValidationGroup == strValidationGroup)
        {
            rfvNamePop.Validate();
            if (rfvNamePop.IsValid == false)
            {
                txtNamePop.ControlStyle.CssClass = "cb-fields validationError";
            }
            else
            {
                txtNamePop.ControlStyle.CssClass = "cb-fields";
            }
        }
        else
        {
            txtNamePop.ControlStyle.CssClass = "cb-fields";
        }

        if (revMobilePop.Enabled && revMobilePop.ValidationGroup == strValidationGroup)
        {
            if (rfvNamePop.IsValid)
            {
                revNamePop.Validate();
                if (revMobilePop.IsValid == false)
                {
                    txtNamePop.ControlStyle.CssClass = "cb-fields validationError";
                }
                else
                {
                    txtNamePop.ControlStyle.CssClass = "cb-fields";
                }
            }
        }
        else
        {
            txtNamePop.ControlStyle.CssClass = "cb-fields";
        }

        //Call Time
        if (rfvCallMeTime.Enabled && rfvCallMeTime.ValidationGroup == strValidationGroup)
        {
            rfvCallMeTime.Validate();
            if (rfvCallMeTime.IsValid == false)
            {
                ddlCallMeTime.ControlStyle.CssClass = "cb-fields-dropdown validationError";
            }
            else
            {
                ddlCallMeTime.ControlStyle.CssClass = "cb-fields-dropdown";
            }
        }
        else
        {
            ddlCallMeTime.ControlStyle.CssClass = "cb-fields-dropdown";
        }

        #endregion

    }

    private void ShowQuoteSummary()
    {
        CarDetailTH _cardetail = new CarDetailTH();
        CarPolicy _carpolicy = new CarPolicy();

        CarDetailTHDB _cardetaildb = new CarDetailTHDB();
        CarPolicyDB _carpolicydb = new CarPolicyDB();

        ProfileDB _profiledb = new ProfileDB();
        Profile _profile = new Profile();

        Int64 _refceuserid = 0;
        Int64 _carid = 0;

        if (Session[General.UserId] != null)
        {
            _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
        }

        if (Session[General.CarId] != null)
        {
            _carid = Convert.ToInt64(Session[General.CarId].ToString());
            _cardetail = _cardetaildb.GetCar(_carid, _refceuserid);
        }
        _carpolicy = _carpolicydb.GetCarPolicy(_carid, _refceuserid);

        if (_cardetail != null && _carpolicy != null)
        {
            lblCarMakeCode.Value = _cardetail.CarMakeName;
            lblCarMakeName.Value = _cardetail.CarMakeName;
            lblCarModelFamilyName.Value = _cardetail.CarModelName.Replace('_', ' ');
            switch (_carpolicy.InsuranceType)
            {
                case 10: //class 1
                    lblInsuranceType.Value = "1";
                    break;
                case 11: //class 1 (low cost)
                    lblInsuranceType.Value = "1+";
                    break;
                case 21: //class 2+
                    lblInsuranceType.Value = "2+";
                    break;
                case 31: //class 3+
                    lblInsuranceType.Value = "3+";
                    break;
                case 30: //class 3
                    lblInsuranceType.Value = "3";
                    break;
            }

        }

        _profile = null;
        _profiledb = null;
        _cardetail = null;
        _cardetaildb = null;
        _carpolicy = null;
        _carpolicydb = null;
    }

    private void ShowCallPopup()
    {
        Int64 UserId = Convert.ToInt64(Session[General.UserId]);
        Profile _profile = new Profile();
        ProfileDB _profiledb = new ProfileDB();
        Contact _contact = new Contact();
        ContactDB _contactdb = new ContactDB();

        _profile = _profiledb.GetProfile(UserId);
        _profiledb = null;

        if (_profile != null)
        {
            //txtCallMeFullNamePop.Text = _profile.FirstName + " " + _profile.SurName;


            _contact = _contactdb.GetContact(UserId);
            if (_contact != null)
            {
                //usrCallMePhoneNo.PhoneNo = _contact.Mobile;
                //txtEmailPop.Text = _contact.Email;
                //txtEmailPop.ReadOnly = true;

            }

            _profile = null;
        }
    }

    private string getProviderLogoCss(string _companyId, string _type)
    {
        string _class = string.Empty;
        if (_type == "result")
        {
            if (_companyId == "11")
            {
                _class = "allianz-logo";
            }
            else if (_companyId == "12")
            {
                _class = "msig-logo";
            }
            else if (_companyId == "16")
            {
                _class = "axa-logo";
            }
            else if (_companyId == "29")
            {
                _class = "lmg-logo";
            }
            else if (_companyId == "31")
            {
                _class = "aig-logo";
            }
            else if (_companyId == "32")
            {
                _class = "bangkok-insurance-logo";
            }
            else if (_companyId == "33")
            {
                _class = "muangthai-logo";

            }
            else if (_companyId == "35")
            {
                _class = "krungthai-logo";

            }
            else if (_companyId == "39")
            {
                _class = "mittare-logo";

            }
            else if (_companyId == "41")
            {
                _class = "ksk-logo";
            }
            else if (_companyId == "43")
            {
                _class = "synmunkong-logo";

            }
            else if (_companyId == "45")
            {
                _class = "viriyah-logo";

            }
            else if (_companyId == "46")
            {
                _class = "tokiomarine-logo";
            }
            else if (_companyId == "51")
            {
                _class = "scsmg-logo";

            }
            else if (_companyId == "58")
            {
                _class = "thaivivat-logo";

            }
            else if (_companyId == "65")
            {
                _class = "dhipaya-logo";

            }
            else if (_companyId == "66")
            {
                _class = "thaipaiboon-logo";

            }
            else if (_companyId == "67")
            {
                _class = "deves-logo";

            }
            else if (_companyId == "68")
            {
                _class = "thaisri-logo";

            }
            else if (_companyId == "69")
            {
                _class = "asset-logo";

            }
        }
        else if (_type == "moreinfo")
        {
            if (_companyId == "11")
            {
                _class = "allianz";
            }
            else if (_companyId == "12")
            {
                _class = "msig";
            }
            else if (_companyId == "16")
            {
                _class = "axa";
            }
            else if (_companyId == "29")
            {
                _class = "lmg";
            }
            else if (_companyId == "31")
            {
                _class = "aig";
            }
            else if (_companyId == "32")
            {
                _class = "bangkok-insurance";
            }
            else if (_companyId == "33")
            {
                _class = "muangthai";

            }
            else if (_companyId == "35")
            {
                _class = "krungthai";

            }
            else if (_companyId == "39")
            {
                _class = "mittare";

            }
            else if (_companyId == "41")
            {
                _class = "ksk";
            }
            else if (_companyId == "43")
            {
                _class = "synmunkong";

            }
            else if (_companyId == "45")
            {
                _class = "viriyah";

            }
            else if (_companyId == "46")
            {
                _class = "tokiomarine";
            }
            else if (_companyId == "51")
            {
                _class = "scsmg";

            }
            else if (_companyId == "58")
            {
                _class = "thaivivat";

            }
            else if (_companyId == "65")
            {
                _class = "dhipaya";

            }
            else if (_companyId == "66")
            {
                _class = "thaipaiboon";

            }
            else if (_companyId == "67")
            {
                _class = "deves";

            }
            else if (_companyId == "68")
            {
                _class = "thaisri";

            }
            else if (_companyId == "69")
            {
                _class = "asset";

            }
        }
        return _class;
    }

    public string GetMotorBrand(string Brand)
    {
        string _strBrandClass = "gluaygluay";
        if (Brand == "AUDI")
        {
            _strBrandClass = "audi";
        }
        else if (Brand == "BMW")
        {
            _strBrandClass = "bmw";
        }
        else if (Brand == "CHER")
        {
            _strBrandClass = "chery";
        }
        else if (Brand == "CHEV")
        {
            _strBrandClass = "chevrolet";
        }
        else if (Brand == "CITR")
        {
            _strBrandClass = "citroen";
        }
        else if (Brand == "DAIH")
        {
            _strBrandClass = "daihatsu";
        }
        else if (Brand == "DFM")
        {
            _strBrandClass = "dfm";
        }
        else if (Brand == "FORD")
        {
            _strBrandClass = "ford";
        }
        else if (Brand == "HOND")
        {
            _strBrandClass = "honda";
        }
        else if (Brand == "HYUN")
        {
            _strBrandClass = "hyundai";
        }
        else if (Brand == "ISUZ")
        {
            _strBrandClass = "isuzu";
        }
        else if (Brand == "JAGU")
        {
            _strBrandClass = "jaguar";
        }
        else if (Brand == "JEEP")
        {
            _strBrandClass = "jeep";
        }
        else if (Brand == "KIA")
        {
            _strBrandClass = "kia";
        }
        else if (Brand == "LAND")
        {
            _strBrandClass = "landrover";
        }
        else if (Brand == "LEXU")
        {
            _strBrandClass = "lexus";
        }
        else if (Brand == "MAZD")
        {
            _strBrandClass = "mazda";
        }
        else if (Brand == "MERC")
        {
            _strBrandClass = "mercedes";
        }
        else if (Brand == "MINI")
        {
            _strBrandClass = "mini";
        }
        else if (Brand == "MITS")
        {
            _strBrandClass = "mitsubishi";
        }
        else if (Brand == "NISS")
        {
            _strBrandClass = "nissan";
        }
        else if (Brand == "PEUG")
        {
            _strBrandClass = "peugeot";
        }
        else if (Brand == "PORS")
        {
            _strBrandClass = "porsche";
        }
        else if (Brand == "PROT")
        {
            _strBrandClass = "proton";
        }
        else if (Brand == "SSAN")
        {
            _strBrandClass = "ssangyong";
        }
        else if (Brand == "SUBA")
        {
            _strBrandClass = "subaru";
        }
        else if (Brand == "SUZU")
        {
            _strBrandClass = "suzuki";
        }
        else if (Brand == "TATA")
        {
            _strBrandClass = "tata";
        }
        else if (Brand == "TOYO")
        {
            _strBrandClass = "toyota";
        }
        else if (Brand == "VOLK")
        {
            _strBrandClass = "volkswagen";
        }
        else if (Brand == "VOLV")
        {
            _strBrandClass = "volvo";
        }
        return _strBrandClass;
    }

    #region Hide on 24 Oct 2014
    //protected void txtEmailPop_TextChanged(object sender, EventArgs e)
    //{
    //    _strNewEmail = "";
    //    _strNewName = "";
    //    _strNewMobile = "";
    //    _strNewUserId = "";
    //    _strUserStatus = "";
    //    lblExistingUser.Text = Session[CE.Global.General.UserEmail].ToString();
    //    if (EmailValidate("callme"))
    //    {
    //        #region Check Customer
    //        Account _useraccount = new Account();
    //        AccountDB _useraccountdb = new AccountDB();

    //        Profile _userprofile = new Profile();
    //        ProfileDB _userprofiledb = new ProfileDB();

    //        Contact _contact = new Contact();
    //        ContactDB _contactdb = new ContactDB();

    //        if (_useraccountdb.IsAlreadyExist(txtEmailPop.Text.Trim().ToLower(), Convert.ToInt32(CE.Global.CEUserType.Customer)))
    //        {
    //            _useraccount = _useraccountdb.GetUserAccount(txtEmailPop.Text.Trim(), 400);

    //            if (_useraccount != null && _useraccount.CEID > 0)
    //            {
    //                _strNewUserId = _useraccount.CEID.ToString();
    //                _strNewEmail = _useraccount.Email;
    //                lblNewUserId.Text = _useraccount.CEID.ToString();
    //                lblNewEmail.Text = _useraccount.Email;

    //                hdnChangeUserId.Value = _useraccount.CEID.ToString();
    //                hdnChangeEmail.Value = _useraccount.Email;
    //                lblExistingUser.Text = Session[CE.Global.General.UserEmail].ToString();
    //                _userprofile = _userprofiledb.GetProfile(_useraccount.CEID);
    //                if (_userprofile != null && _userprofile.ProfileId > 0)
    //                {
    //                    if (_userprofile.FirstName != string.Empty)
    //                    {
    //                        lblChangeUser.Text = _userprofile.FirstName + " " + _userprofile.SurName;
    //                        hdnNewName.Value = _userprofile.FirstName + " " + _userprofile.SurName;
    //                    }
    //                    else
    //                    {
    //                        lblChangeUser.Text = _userprofile.SurName;
    //                        hdnNewName.Value = _userprofile.SurName;
    //                    }
    //                }
    //                _contact = _contactdb.GetContact(_useraccount.CEID);
    //                if (_contact != null && _contact.Mobile.Length > 0)
    //                {
    //                    hdnChangeMobile.Value = _contact.Mobile;

    //                }
    //                _strNewName = lblChangeUser.Text.Trim();
    //                _strNewMobile = hdnChangeMobile.Value;
    //                lblNewName.Text = lblChangeUser.Text.Trim();
    //                lblNewMobile.Text = hdnChangeMobile.Value;
    //                pnlCallMeDetails.Visible = false;
    //                pnlUserChange.Visible = true;
    //                pnlUserUpdate.Visible = false;
    //                mpeCallPop.Show();
    //            }
    //        }
    //        else
    //        {
    //            pnlCallMeDetails.Visible = true;
    //            pnlUserUpdate.Visible = false;
    //            pnlUserChange.Visible = false;
    //            mpeCallPop.Show();
    //        }
    //        #endregion
    //    }
    //}

    //public bool EmailValidate(string strValidationGroup)
    //{
    //    bool _isvalid = false;
    //    if (rfvEmailPop.Enabled && rfvEmailPop.ValidationGroup == strValidationGroup)
    //    {
    //        rfvEmailPop.Validate();
    //        if (rfvEmailPop.IsValid == false)
    //        {
    //            txtEmailPop.ControlStyle.CssClass = "cb-fields-1 validationError";
    //        }
    //        else
    //        {
    //            txtEmailPop.ControlStyle.CssClass = "cb-fields-1";
    //        }
    //    }
    //    else
    //    {
    //        txtEmailPop.ControlStyle.CssClass = "cb-fields-1";
    //    }

    //    if (revEmailPop.Enabled && revEmailPop.ValidationGroup == strValidationGroup)
    //    {
    //        if (rfvEmailPop.IsValid)
    //        {
    //            revEmailPop.Validate();
    //            if (revEmailPop.IsValid == false)
    //            {
    //                txtEmailPop.ControlStyle.CssClass = "cb-fields-1 validationError";
    //            }
    //            else
    //            {
    //                txtEmailPop.ControlStyle.CssClass = "cb-fields-1";
    //            }
    //        }
    //    }
    //    else
    //    {
    //        txtEmailPop.ControlStyle.CssClass = "cb-fields-1";
    //    }
    //    if (!rfvEmailPop.Enabled)
    //    {
    //        _isvalid = true;
    //    }
    //    else if (rfvEmailPop.IsValid)
    //    {
    //        if (!revEmailPop.Enabled)
    //        {
    //            _isvalid = true;
    //        }
    //        else if (revEmailPop.IsValid)
    //        {
    //            _isvalid = true;
    //        }
    //    }
    //    return _isvalid;
    //}

    //protected void btnChangeYes_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        lblUserStatus.Text = "CHANGE";
    //        txtMobilePop.Text = lblNewMobile.Text.Trim();
    //        pnlCallMeDetails.Visible = true;
    //        pnlUserChange.Visible = false;
    //        pnlUserUpdate.Visible = false;
    //        mpeCallPop.Show();
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //}

    //protected void btnChangeNo_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        lblUserStatus.Text = "";
    //        txtEmailPop.Text = Session[CE.Global.General.UserEmail].ToString();
    //        txtMobilePop.Text = Session[CE.Global.General.UserPhoneNo].ToString();
    //        pnlCallMeDetails.Visible = true;
    //        pnlUserChange.Visible = false;
    //        pnlUserUpdate.Visible = false;
    //        mpeCallPop.Show();
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //}

    //protected void btnUpdateYes_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        lblUserStatus.Text = "UPDATE";
    //        _strUserStatus = lblUserStatus.Text.Trim();
    //        lblNewName.Text = Session[General.UserName].ToString();
    //        lblNewMobile.Text = Session[General.UserPhoneNo].ToString();
    //        lblNewUserId.Text = Session[General.UserId].ToString();
    //        string CallMeUserEmail = Session[General.UserEmail].ToString();
    //        if (txtMobilePop.Text.Trim().Length == 10)
    //        {
    //            string ExistingEmail = Session[General.UserEmail].ToString();
    //            string _message = "";
    //            string _title = "";
    //            string CallMeFullNamePop = "";
    //            string strInsType = "";
    //            if (_strUserStatus == "UPDATE")
    //            {
    //                Int64 _CarId = 0;
    //                Int64 _UserId = 0;
    //                Int64 _ExistingUserId = 0;
    //                if (!Int64.TryParse(Session[General.CarId].ToString(), out _CarId))
    //                {
    //                    _CarId = 0;
    //                }
    //                if (!Int64.TryParse(lblNewUserId.Text.Trim(), out _UserId))
    //                {
    //                    _UserId = 0;
    //                }
    //                if (!Int64.TryParse(Session[General.UserId].ToString(), out _ExistingUserId))
    //                {
    //                    _ExistingUserId = 0;
    //                }
    //                UpdateUserIdCarDetails(_CarId, _UserId, _ExistingUserId, Session[General.UserEmail].ToString(), _strUserStatus, txtEmailPop.Text.Trim(), lblNewName.Text.Trim(), txtMobilePop.Text.Trim());
    //                Session[CE.Global.General.UserId] = lblNewUserId.Text.Trim();
    //                Session[CE.Global.General.UserTypeId] = "400";
    //                Session[CE.Global.General.UserEmail] = txtEmailPop.Text.Trim();
    //                Session[CE.Global.General.UserPhoneNo] = txtMobilePop.Text.Trim();
    //                Session[CE.Global.General.StartTime] = General.GetUTCDateTime().ToString();
    //                Session[CE.Global.General.UserName] = lblNewName.Text.Trim();
    //                Session[CE.Global.General.UserProfileAndContactComplete] = "1";
    //                SetCookies(lblNewName.Text.Trim(), txtEmailPop.Text.Trim(), txtMobilePop.Text.Trim());
    //            }
    //            CallMeFullNamePop = Session[General.UserName].ToString();
    //            _message = "Customer Name: " + Server.HtmlEncode(CallMeFullNamePop);
    //            _title = "Call Me Now - Call Me request by " + Server.HtmlEncode(CallMeFullNamePop);
    //            ChangeValidationEffectServerSide("callme");
    //            if (IsValid)
    //            {
    //                Int64 _result = 0;
    //                ContactUser _cu = new ContactUser();
    //                ContactUserDB _cudb = new ContactUserDB();

    //                strInsType = lblInsuranceType.Value;

    //                _cu.InsuranceType = "CI-" + strInsType;


    //                _cu.ProductId = Convert.ToInt64(Session[General.CarId].ToString());
    //                _cu.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

    //                _cu.ProviderCompanyId = Convert.ToInt64(hidProvider.Value);
    //                _cu.ContactNo = txtMobilePop.Text.Trim();
    //                _cu.FullName = Server.HtmlEncode(CallMeFullNamePop);
    //                _cu.CallTime = Convert.ToInt16(ddlCallMeTime.Text);

    //                if (Request.Params["spc"] != null)
    //                {
    //                    if (Request.Params["spc"].ToString() == "1")
    //                    {
    //                        _cu.Info1 = "GG Promotion";
    //                    }
    //                }

    //                _result = _cudb.Insert(_cu);

    //                if (_result > 0)
    //                {
    //                    Profile _profile = new Profile();
    //                    ProfileDB _profiledb = new ProfileDB();
    //                    _profile = _profiledb.GetProfile(_cu.RefCEUserId);
    //                    if (_profile != null && _profile.ProfileId > 0)
    //                    {
    //                        _profile.SurName = string.Empty;
    //                        _profile.FirstName = Server.HtmlEncode(CallMeFullNamePop);
    //                        _profiledb.Insert(_profile);
    //                    }
    //                    _profile = null;
    //                    _profiledb = null;

    //                    Contact _contact = new Contact();
    //                    ContactDB _contactdb = new ContactDB();
    //                    _contact = _contactdb.GetContact(_cu.RefCEUserId);
    //                    if (_contact != null && _contact.ContactId > 0)
    //                    {
    //                        _contact.Mobile = txtMobilePop.Text.Trim();
    //                        _contactdb.Insert(_contact);
    //                    }
    //                    _contact = null;
    //                    _contactdb = null;

    //                    CE.CarInsurance.CarDetailTH _cardetail = new CE.CarInsurance.CarDetailTH();
    //                    CE.CarInsurance.CarDetailTHDB _cardetailDB = new CE.CarInsurance.CarDetailTHDB();
    //                    _cardetail = _cardetailDB.GetCar(_cu.ProductId, _cu.RefCEUserId);
    //                    if (_cardetail != null && _cardetail.CarId > 0)
    //                    {
    //                        _cardetail.Info7 = string.Empty;
    //                        _cardetail.Info8 = Server.HtmlEncode(CallMeFullNamePop);
    //                        _cardetailDB.Insert(_cardetail);
    //                    }
    //                    _cardetail = null;
    //                    _cardetailDB = null;
    //                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "alertThxU", "alert('" + Resources.CarInsurance.CallMeMessage + "')", true);

    //                    _message += "<img src='http://www.gluaygluay.com/images/callme/callme-home.png' /><br/>";
    //                    _message += "<br/><br/>Insurance Type : " + strInsType;
    //                    _message += "<br/><br/>Full Name : " + Server.HtmlEncode(CallMeFullNamePop);
    //                    _message += "<br/><br/>Email : " + txtEmailPop.Text;
    //                    _message += "<br/><br/>Contact No : " + txtMobilePop.Text.Trim();
    //                    _message += "<br/><br/>Call Time : " + ddlCallMeTime.SelectedItem.Text;
    //                    string _emailList = ConfigurationManager.AppSettings["TH.CICallMeMessageReceiver"].ToString();
    //                    MyMailManager.SendCRMMessage(_emailList, _title, _message);

    //                    MyMailManager.SendEmailChangeNotification(CallMeFullNamePop, txtEmailPop.Text.Trim(), ExistingEmail);
    //                    txtEmailPop.ReadOnly = true;
    //                    ddlCallMeTime.Enabled = false;
    //                    txtMobilePop.ReadOnly = true;
    //                    btnSubmit.Enabled = false;
    //                    btnCancel_Click(null, null);
    //                    Response.Redirect(CE.Global.General.GetUrl("/car-insurance/compare-insurance", Page));
    //                }
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //}

    //protected void btnUpdateNo_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        lblUserStatus.Text = "";
    //        txtEmailPop.Text = Session[CE.Global.General.UserEmail].ToString();
    //        txtMobilePop.Text = Session[CE.Global.General.UserPhoneNo].ToString();
    //        pnlCallMeDetails.Visible = true;
    //        pnlUserChange.Visible = false;
    //        pnlUserUpdate.Visible = false;
    //        mpeCallPop.Show();
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //}
    #endregion

    private void UpdateUserIdCarDetails(Int64 CarId, Int64 UserId, Int64 ExistingUserId, string ExistingEmail, string ChangeStatus, string Email, string Name, string Mobile)
    {
        try
        {
            Int64 _result = -1;
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            string _spName = "sprocCarDetailUpdateSingleItemByUserId";
            SortedList _sPara = new SortedList();
            _sPara.Add("@CarId", CarId);
            _sPara.Add("@RefCEUserId", UserId);
            _sPara.Add("@ExistingUserId", ExistingUserId);
            _sPara.Add("@ExistingEmail", ExistingEmail);
            _sPara.Add("@ChangeStatus", ChangeStatus);
            _sPara.Add("@Email", Email);
            _sPara.Add("@Name", Name);
            _sPara.Add("@Mobile", Mobile);

            _result = _db.ExecuteNonSqlSP(_spName, _sPara);
        }
        catch (Exception ex)
        {
        }
    }

    private void SetCookies(string UserName, string UserEmail, string UserMobile)
    {
        try
        {
            HttpCookie _cookie = new HttpCookie("GGQuote");
            _cookie.Values.Add("CustomerName", MySecurity.Encrypt(UserName, true));
            _cookie.Values.Add("CustomerEmail", MySecurity.Encrypt(UserEmail, true));
            _cookie.Values.Add("CustomerMobile", MySecurity.Encrypt(UserMobile, true));

            _cookie.Expires = DateTime.Now.AddDays(90);
            Response.Cookies.Add(_cookie);
        }
        catch (Exception Ex)
        {
            Ex = null;
        }
    }

    private Int64 UpdateProfile(Int64 UserId, Int64 CarId)
    {
        Int64 _retValue = 0;

        if (UserId != 0)
        {
            Profile _profile = new Profile();
            ProfileDB _profiledb = new ProfileDB();

            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();

            _profile = _profiledb.GetProfile(UserId);
            _contact = _contactdb.GetContact(UserId);
            _retValue = UserId;
            if (_profile != null)
            {
                #region Update Profile

                //_profile.Salutation = "";
                _profile.FirstName = HttpUtility.HtmlEncode(txtNamePop.Text);
                //_profile.SurName = "";
                //if (_profile.DOB.Year == 1900)
                //{
                //    DateTime dteDOB = new DateTime(1995, 1, 1);
                //    _profile.DOB = dteDOB;
                //}
                //if (_profile.Gender.ToString().Trim() == "")
                //{
                //    _profile.Gender = 'M';
                //}
                _profile.Status = "Complete";

                _retValue = _profiledb.Insert(_profile);

                if (_contact != null)
                {
                    _contact.Mobile = txtMobilePop.Text.Trim();
                    _contact.Status = true;

                    _retValue = _contactdb.Insert(_contact);
                }

                if (_retValue > 0)
                {
                    if (_profile.FirstName != string.Empty)
                    {
                        Session[CE.Global.General.UserName] = _profile.FirstName + " " + _profile.SurName;
                    }
                    else
                    {
                        Session[CE.Global.General.UserName] = _profile.SurName;
                    }
                    if (_contact != null)
                    {
                        Session[General.UserPhoneNo] = _contact.Mobile;
                    }
                    Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                    if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) == Convert.ToInt32(CE.Global.CEUserType.Provider))
                    {
                        Session[CE.Global.General.ProviderProfileFirstTime] = "0";
                    }
                    Session[CE.Global.General.IsAcceptTC] = "0";
                    if (_profile.IsAcceptTC == true)
                    {
                        Session[CE.Global.General.IsAcceptTC] = "1";
                    }
                }

                _profile = null;
                _contact = null;
                #endregion
            }
        }
        else
        {
            _retValue = SaveNewCustomer();
            UserId = _retValue;
        }

        if (_retValue > 0 && CarId > 0)
        {
            CarDetailTH _cardetail = new CarDetailTH();
            CarDetailTHDB _cardetaildb = new CarDetailTHDB();
            _cardetail = _cardetaildb.GetCar(CarId, UserId);


            Profile _profile = new CE.User.Profile();
            ProfileDB _profiledb = new ProfileDB();
            _profile = _profiledb.GetProfile(UserId);

            if (_cardetail != null)
            {
                _cardetail.Info5 = txtEmailPop.Text.Trim().ToLower();
                _cardetail.Info6 = txtMobilePop.Text.Trim();
                _cardetail.Info7 = ""; //txtSurName.Text.Trim();
                _cardetail.Info8 = Session[CE.Global.General.UserName].ToString();
                if (_profile.Gender.ToString().Trim() != "")
                {
                    _cardetail.Info9 = _profile.Gender.ToString();
                }
                else
                {
                    _cardetail.Info9 = "M";
                }
                if (_profile.DOB.Year == 1900)
                {
                    DateTime dteDOB = new DateTime(1995, 1, 1);
                    _cardetail.Info10 = dteDOB.ToString("dd-MMM-yyyy");
                }
                else
                {
                    _cardetail.Info10 = _profile.DOB.ToString("dd-MMM-yyyy");
                }
                _cardetail.Info11 = _profile.Occupation.ToString();
                _cardetail.Info12 = "";// ddlJobNature.GetJobNature();
                _cardetail.Info13 = "";// ddlMaritalStatus.GetMaritalStatus();
                _cardetail.Info14 = "";// ddlYearDriveSg.Text;
                _cardetail.Info15 = "";// ddlSalutation.GetSalutation();
                _cardetail.RefCEUserId = UserId;
            }
            _profile = null;

            //RefUserId only update
            CarPolicy _carpolicy = new CarPolicy();
            CarPolicyDB _carpolicydb = new CarPolicyDB();
            _carpolicy = _carpolicydb.GetCarPolicy(CarId, UserId);
            _carpolicy.RefCEUserId = UserId;

            _retValue = _cardetaildb.Insert(_cardetail);

            _retValue = _carpolicydb.Insert(_carpolicy);

            //update RefIds
            CarDetailTHDB cardb = new CarDetailTHDB();
            cardb.UpdateCarDetailPolicy(CarId, UserId);

            if (_retValue <= 0)
            {
            }
        }

        return _retValue;
    }

    private Int64 SaveNewCustomer()
    {
        #region New Customer
        Int64 _retVal = 0;
        if (Page.IsValid)
        {

            Account _useraccount = new Account();
            AccountDB _useraccountdb = new AccountDB();

            Profile _userprofile = new Profile();
            ProfileDB _userprofiledb = new ProfileDB();

            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();

            _useraccount = _useraccountdb.GetUserAccount(txtEmailPop.Text.Trim(), Convert.ToInt32(CE.Global.CEUserType.Customer));
            //Hide on 13 Nov 2014 //_useraccount = _useraccountdb.GetUserAccount(txtEmailPop.Text.Trim(), txtMobilePop.Text.Trim().ToLower(), Convert.ToInt32(CE.Global.CEUserType.Customer));
            if (_useraccount != null)
            {
                if (_useraccount.CEID > 0)
                {
                    _retVal = _useraccount.CEID;
                    _contact = _contactdb.GetContact(_useraccount.CEID);
                    Session[CE.Global.General.UserId] = _useraccount.CEID;
                    Session[CE.Global.General.UserTypeId] = _useraccount.UserTypeId;
                    Session[CE.Global.General.UserEmail] = _useraccount.Email;
                    Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                    Session[CE.Global.General.StartTime] = General.GetUTCDateTime().ToString();
                    _userprofile = _userprofiledb.GetProfile(_useraccount.CEID);
                    if (_userprofile != null && _userprofile.ProfileId > 0)
                    {
                        if (_userprofile.FirstName != string.Empty)
                        {
                            Session[CE.Global.General.UserName] = _userprofile.FirstName + " " + _userprofile.SurName;
                        }
                        else
                        {
                            Session[CE.Global.General.UserName] = _userprofile.SurName;
                        }
                    }
                }
            }
            else
            {
                long _result = -1;
                _useraccount = new Account();
                _useraccount.Email = txtEmailPop.Text.Trim().ToLower();
                string _authcode = txtMobilePop.Text.Trim();
                _useraccount.AuthCode = _authcode;
                _useraccount.UserId = "CEUSERAUTOIDCI";
                _useraccount.UserTypeId = Convert.ToInt32(CE.Global.CEUserType.Customer);
                _useraccount.IsAcceptTC = false;
                _useraccount.Status = true;
                _useraccount.RefCEUserID = -1;
                _result = _useraccountdb.Insert(_useraccount);
                _useraccount.CEID = _result;
                _retVal = _useraccount.CEID;
                if (_result > 0)
                {
                    _userprofile.Salutation = "";
                    _userprofile.FirstName = Server.HtmlEncode(txtNamePop.Text.Trim());
                    _userprofile.SurName = "";
                    DateTime dteDOB = new DateTime(1995, 1, 1);
                    _userprofile.DOB = dteDOB;
                    _userprofile.Gender = 'M';

                    _userprofile.Occupation = Convert.ToInt32("54");
                    _userprofile.Status = "Complete";
                    _userprofile.RefCEUserId = _useraccount.CEID;
                    _userprofile.IsAcceptTC = checkIsAgreement.Checked;
                    _result = _userprofiledb.Insert(_userprofile);

                    _contact.Email = _useraccount.Email;
                    _contact.Mobile = txtMobilePop.Text.Trim();

                    _contact.Status = true;
                    _contact.RefCEUserId = _useraccount.CEID;
                    _result = _contactdb.Insert(_contact);

                    if (_result > 0)
                    {
                        Session[CE.Global.General.UserId] = _useraccount.CEID;
                        Session[CE.Global.General.UserTypeId] = _useraccount.UserTypeId;
                        Session[CE.Global.General.UserEmail] = _useraccount.Email;
                        Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                        Session[CE.Global.General.StartTime] = General.GetUTCDateTime().ToString();
                        if (_userprofile.FirstName != string.Empty)
                        {
                            Session[CE.Global.General.UserName] = _userprofile.FirstName + " " + _userprofile.SurName;
                        }
                        else
                        {
                            Session[CE.Global.General.UserName] = _userprofile.SurName;
                        }
                        Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                        Session[CE.Global.General.IsAcceptTC] = "0";
                        if (_userprofile.IsAcceptTC == true)
                        {
                            Session[CE.Global.General.IsAcceptTC] = "1";
                        }
                    }
                }

            }
        }
        else
        {
            _retVal = 0;
        }

        return _retVal;
        #endregion
    }

    private bool SendMailToUser(string EmailAddress, string UserName, string AuthCode)
    {
        bool _retVal = false;
        _retVal = CE.Email.Welcome.SendMail(EmailAddress, UserName, AuthCode);
        return _retVal;
    }

    public Int32 CheckCallMeCount()
    {
        try
        {
            string CarId = "0";
            string UserId = "0";
            if (Session[CE.Global.General.CarId] != null)
            {
                CarId = Session[CE.Global.General.CarId].ToString();
            }
            if (Session[CE.Global.General.UserId] != null)
            {
                UserId = Session[CE.Global.General.UserId].ToString();
            }
            DataTable _dt = new DataTable();
            MyDatabase _db = new MyDatabase();
            string _sqlString = @"SELECT COUNT(*) RECORDCOUNT FROM tblContactUser WHERE ProductId = " + CarId + " AND RefCEUserId = " + UserId;
            _sqlString += " AND (CONVERT(VARCHAR(8),ModifiedDate,112) BETWEEN CONVERT(VARCHAR(8),DATEADD(DD, -60,dbo.fn_getUTCDateTime()),112) ";
            _sqlString += " AND CONVERT(VARCHAR(8),dbo.fn_getUTCDateTime(),112))";
            _dt = _db.GetDataTable(_sqlString);
            _db = null;

            string rtnCount = "0";
            Int32 _retResult = 0;
            if (_dt != null && _dt.Rows[0]["RECORDCOUNT"] != null)
            {
                rtnCount = _dt.Rows[0]["RECORDCOUNT"].ToString();
            }
            if (rtnCount != null)
            {
                if (!Int32.TryParse(rtnCount.ToString(), out _retResult))
                {
                    _retResult = 0;
                }
            }
            return _retResult;
        }
        catch (Exception ex)
        {
            return -1;
        }
    }

    private void GetPromotionDetails(Int64 CarId)
    {
        try
        {
            #region TrueYou Promotion
            hdnIsPromo.Value = "0";
            hdnPromoId.Value = "0";
            hdnPromoCode.Value = "0";
            string _TruePromo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
            Int64 _intTruePromo = 0;
            Int16 IsTruePromo = 0;
            if (!Int64.TryParse(_TruePromo, out _intTruePromo))
            {
                _intTruePromo = 0;
            }
            if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
            {
                if (_intTruePromo >= 20150814 && _intTruePromo <= 20151031)
                {
                    hdnIsPromo.Value = "1";
                    IsTruePromo = 1;
                }
            }
            else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
            {
                if (_intTruePromo >= 25580814 && _intTruePromo <= 25581031)
                {
                    hdnIsPromo.Value = "1";
                    IsTruePromo = 1;
                }
            }
            if (IsTruePromo == 1)
            {
                MyDatabase _db = new MyDatabase();
                DataTable _dtResult = new DataTable();
                string _spName = "sprocGetPromoDetailsSelectedItem";
                SortedList _sPara = new SortedList();
                _sPara.Add("@CarId", CarId);
                _dtResult = _db.GetDataTableBySP(_spName, _sPara);
                if (_dtResult != null && _dtResult.Rows.Count > 0)
                {
                    Int16 PromoId = 0;
                    if (!Int16.TryParse(_dtResult.Rows[0]["Promotion"].ToString(), out PromoId))
                    {
                        PromoId = 0;
                    }
                    hdnPromoId.Value = PromoId.ToString();
                    hdnPromoCode.Value = _dtResult.Rows[0]["PromoCode"].ToString();
                }
            }
            #endregion
        }
        catch (Exception ex)
        {
        }
    }

}
